/*
* Yahtzee 3.0 Client
* (c) Karl Puusepp & Tony Lepmets
* maiz & co, 2012
*/

import java.io.BufferedReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class YahtzeeClient {
	
	YahtzeeIO io;
	YahtzeePanel panel;
	GamePanel gamePanel;
	ScoreTable table;
	
	YahtzeeClient(BufferedReader in, PrintWriter out) {
		io = new YahtzeeIO(in, out);
		gamePanel = new GamePanel(this);
		table = new ScoreTable(this);
		panel = new YahtzeePanel(gamePanel, table);
	}
	
	public void play() throws Exception {
		
		// handshake
		String playerName = io.askPlayerName("Sisesta oma nimi tühikute ja täpitähtedeta.");
		while(!playerName.matches("\\A\\p{ASCII}*\\z") || playerName.contains(" ")) {
			playerName = io.askPlayerName("Tõsiselt ka, sisesta oma nimi tühikute ja täpitähtedeta.");
		}
		
		io.send(playerName);
		String response = io.receiveString();
		if (response.equals("Hello "+playerName+", welcome to play! Y31"))
			System.out.println("handshake complete");
		else
			throw new Exception("handshake received wrong input from server");
		
		System.out.print("Waiting for other contestants...");
		StringTokenizer score = io.receive(); // get player list to confirm this
		System.out.println("done.");
		table.createTable(score);
		
		StringTokenizer st = null;
		
		while ((st = io.receive()) != null) {
			
			gamePanel.veereta.setEnabled(true);
			gamePanel.text.setText("Sinu kord. Esimene veeretus.");
			gamePanel.veereta.setText("Veereta!");
			
			// get scoreboard before throw
			score=io.receive(); // comes in as "Karl 0 0 0 0 0 ... 0 "
			table.updateTable(score);
			
			// first throw
			// input comes in form "Karl 1 4 2 4 6"
			st.nextToken(); // player name
			parseDice(st); // dice information gets sent to dice panel
			
			/* outgoing dice selection information is sent via button */
		
			// second throw	
			st = io.receive();
			gamePanel.text.setText("Sinu kord. Teine veeretus.");
			st.nextToken(); // player name
			parseDice(st); // dice information gets sent to dice panel
		
			// final throw
			st = io.receive();
			st.nextToken(); // player name
			gamePanel.text.setText("Kolmas veeretus. Vali lahter.");
			parseDice(st); // send dice info to panel
			
			table.setEnabled(true);
			
			gamePanel.veereta.setText("Vali lahter.");
			gamePanel.veereta.setEnabled(false);
			
			// get updated scoreboard
			score=io.receive();
			table.updateTable(score);
			
			gamePanel.text.setText("Oota enda korda.");
			gamePanel.veereta.setText("Oota...");
			parseDice(new StringTokenizer("0 0 0 0 0"));
			for (int i = 0; i < 5; i++)
				gamePanel.userSelection[i]=0; // untoggle checkboxes
			
			
		}
		
		score=io.receive();
		table.updateTable(score);
		
		String finalMsg = io.receiveString();
		gamePanel.text.setText(finalMsg);
		gamePanel.veereta.setText("Mäng läbi.");
		
	}
	
	void parseDice(StringTokenizer serverSentDice) {
		for (int i = 0; i < 5 ; i++) {
			gamePanel.dice[i] = Integer.parseInt(serverSentDice.nextToken());
		}
		gamePanel.updateDicePanel();
	}
	
	String selectionToString() {
		String result = "";
		for (int elem : gamePanel.userSelection)
			result+=elem+" ";
		return result;
	}
}
